package com.hbm.usercenter.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.hbm.usercenter.MainActivity;
import com.hbm.usercenter.R;
import com.hbm.usercenter.base.BaseFragmentCommon;
import com.hbm.usercenter.conf.Constants;
import com.hbm.usercenter.http.MyCallBack;
import com.hbm.usercenter.http.NetClient;
import com.hbm.usercenter.utils.CommonUtils;
import com.hbm.usercenter.utils.LogUtils;
import com.hbm.usercenter.utils.UIUtils;
import com.hbm.usercenter.view.CountdownButton;
import com.hbm.usercenter.widget.GoupInputVerify;
import com.hbm.usercenter.widget.GroupInput;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class PhLoginFragment extends BaseFragmentCommon {


    @Bind(R.id.toolbar_title)
    TextView mToolbarTitle;
    @Bind(R.id.phone)
    GoupInputVerify mPhone;
    @Bind(R.id.verify)
    GroupInput mVerify;
    @Bind(R.id.btn_login)
    Button mBtnLogin;
    @Bind(R.id.btn_timer)
    CountdownButton mBtnTimer;
    private static String mpNum;
    private String mCode;

    public static String getName() {
        return  mpNum;
    }
    @Override
    public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_ph_login, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void initData() {
        mToolbarTitle.setText("惠学科技会员登录");
    }

    @Override
    protected void initListener() {
        mPhone.setClick(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mpNum = mPhone.getPhone();
                checkPhone(mpNum);
                if (checkPhone(mpNum)) {
                    mPhone.setIsCount(true);
                    getCode();
                } else {
                    mPhone.setIsCount(false); ;
                }
            }
        });
    }

    private boolean checkPhone(String mpNum) {
        if (!CommonUtils.isChinaPhoneLegal(mpNum)) {
            Toast.makeText(UIUtils.getContext(), "请输入正确的手机号", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    public void getCode() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("action", "login");
        params.put("phone", mpNum);
        NetClient.getNetClient().callNet(Constants.URLS.SENDCODEURL, new MyCallBack() {
            @Override
            public void onFailure(IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(String json) {
                processResult(json, false);
                LogUtils.d(json);

            }
        }, params);
    }

    @OnClick({R.id.phone, R.id.btn_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                mCode = mVerify.getText();
                sendRequest();
                break;
        }
    }

    private void sendRequest() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("sms_verify", mCode);
        params.put("phone", mpNum);
        NetClient.getNetClient().callNet(Constants.URLS.PHLOGINURL, new MyCallBack() {
            @Override
            public void onFailure(IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(String json) {
                processResult(json, true);
                LogUtils.d(json);

            }
        }, params);

    }


    private void processResult(String json, boolean isLogin) {
        JSONObject rootObject = null;
        try {
            rootObject = new JSONObject(json);
            final String err_msg = rootObject.getString("err_msg");
            String err_code = rootObject.getString("err_code");
            if (err_code.equals("-1")) {
                //                Intent intent = new Intent(getActivity(), QRCodeActivity.class);
                //                intent.putExtra("money", number);
                //                //                intent.putExtra("money", money);
                //                //                intent.putExtra("picUrl", picUrl);
                //                //
                //                startActivity(intent);
                if (isLogin) {
                    startActivity(new Intent(getActivity(), MainActivity.class));

                } else {

                }
            } else {
                UIUtils.postTaskSafely(new Runnable() {
                    @Override
                    public void run() {
                        CommonUtils.showShort(UIUtils.getContext(), err_msg);
                    }
                });
                //                Toast.makeText(UIUtils.getContext(), err_msg, Toast
                // .LENGTH_SHORT).show();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }


}
